package com.fenghuajueli.lib_pictureselect.utils;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ConvertUtils;


public class HorizontalItemDecoration extends RecyclerView.ItemDecoration {

    private int spacing;
    private int leftMargin;
    private int rightMargin;

    public HorizontalItemDecoration(int spacing) {
        this.spacing = spacing;
        this.leftMargin = ConvertUtils.dp2px(14);
        this.rightMargin = ConvertUtils.dp2px(14);
    }

    public HorizontalItemDecoration(int spacing, int leftMargin, int rightMargin) {
        this.spacing = spacing;
        this.leftMargin = leftMargin;
        this.rightMargin = rightMargin;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view);
        if (position == 0) {
            outRect.left = leftMargin;
            outRect.right = spacing;
            outRect.top = 0;
            outRect.bottom = 0;
        } else if (position == parent.getAdapter().getItemCount()) {
            outRect.left = leftMargin;
            outRect.right = rightMargin;
            outRect.top = 0;
            outRect.bottom = 0;
        } else {
            outRect.left = 0;
            outRect.right = spacing;
            outRect.top = 0;
            outRect.bottom = 0;
        }
    }
}